package com.ppc.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ppc.context.BaseContext;
import com.ppc.entity.Claim;
import com.ppc.mapper.worker.WorkerClaimMapper;
import com.ppc.mapper.worker.WorkerMapper;
import com.ppc.result.PageResult;
import com.ppc.service.WorkerClaimService;
import com.ppc.vo.ClaimDetailVO;
import com.ppc.vo.ClaimMessageVO;
import com.ppc.vo.ClaimPageVO;
import java.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 工人申诉接口实现
 *
 * @author Jeferry
 * @date 2023/12/20
 */
@Service
public class WorkerClaimServiceImpl implements WorkerClaimService {

  @Autowired
  private WorkerClaimMapper claimMapper;
  @Autowired
  private WorkerMapper workerMapper;

  /**
   * 创建申诉
   *
   * @param taskId      任务id
   * @param claimType   申诉类型
   * @param claimReason 申诉原因
   */
  @Override
  public void createClaim(Long taskId, Integer claimType, String claimReason) {
    Long notifyId = claimMapper.queryNotifyId(taskId);
    Claim claim = Claim.builder().
        taskId(taskId).
        claimerId(BaseContext.getCurrentId()).//放工人的userid
            claimerType(1).
        claimType(claimType).
        claimReason(claimReason).
        claimStartTime(LocalDateTime.now()).
        claimStatus(0).
        notifyId(notifyId).//放雇主的employerid
            build();
    //调用mapper层进行插入claim
    claimMapper.insert(claim);
    //修改task对应的状态
    //claimtype=4,状态改成52，claimtype=5，状态改成53
    if(claimType==4)
    { Integer taskStatus=52;
      claimMapper.changeTaskStatusIntoClaim(taskId,taskStatus);
    } else if (claimType==5) {
      Integer taskStatus=53;
      claimMapper.changeTaskStatusIntoClaim(taskId,taskStatus);
    }
  }

  /**
   * 查询申诉列表
   *
   * @param page        页码
   * @param pageSize    页大小
   * @param claimStatus 申诉状态
   * @return {@link PageResult}
   */
  @Override
  public PageResult queryClaimList(Integer page, Integer pageSize, Integer claimStatus) {
    //只查询跟自己有关的申诉
    Long userId = BaseContext.getCurrentId();
    Long currentWorkerId = workerMapper.getCurrentWorkerId(userId);
    PageHelper.startPage(page, pageSize);
    Page<ClaimPageVO> p = claimMapper.pageQuery(claimStatus,userId);
    return new PageResult(p.getTotal(), p.getResult());
  }

  /**
   * 查询申诉详情
   *
   * @param claimId 申诉id
   * @return {@link ClaimDetailVO}
   */
  @Override
  public ClaimDetailVO queryClaimDetail(Long claimId) {
    ClaimDetailVO claim = claimMapper.queryClaimDetail(claimId);
    return claim;
  }

  /**
   * 获取申诉消息
   *
   * @param page     页码
   * @param pageSize 页大小
   * @return {@link PageResult}
   */
  @Override
  public PageResult getClaimMessage(Integer page, Integer pageSize) {
    Long currentUserId = BaseContext.getCurrentId();
    Long currentWorkerId = workerMapper.getCurrentWorkerId(currentUserId);
    //进行分页查询
    PageHelper.startPage(page, pageSize);
    Page<ClaimMessageVO> p = claimMapper.getClaimMessage(currentUserId, currentWorkerId);
    return new PageResult(p.getTotal(), p.getResult());
  }
}
